package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrdersMapper ordersMapper;

    /**
     * 处理15分钟未付款超时的订单
     */
    @Scheduled(cron = "0 * * * * ?")
    public void getRefreshScheduledTime(){
        log.info("处理支付超时的订单：{}",new Date());
        LocalDateTime time = LocalDateTime.now().minusMinutes(15);
        log.info("现在时间是{}",time);
        //查询在未支付时间中的订单
        List<Orders> orders = ordersMapper.selectStatus(Orders.PENDING_PAYMENT,time);
        //遍历更改状态
        for (Orders order : orders) {
            //更改这些数据的状态
            order.setStatus(Orders.CANCELLED);
            ordersMapper.updateOrdersStstus(order);
        }
    }


    /**
     * 处理派送中的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void Delivering(){
        log.info("每天凌晨1点处理还在派送中的订单");
        //一小时之前的时间
        LocalDateTime time = LocalDateTime.now().minusMinutes(60);
        //进行修改，只要在一小时之前的订单还在派送中的都设置为已完成
        ordersMapper.update(Orders.COMPLETED, time);
    }
}
